package com.aiyige.page.my.favorite;


import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aiyige.MyApp;
import com.aiyige.R;
import com.aiyige.base.BaseFragment;
import com.aiyige.base.api.JsonUtil;
import com.aiyige.model.Subject;
import com.aiyige.model.moment.entity.FavoriteRecord;
import com.aiyige.model.request.MyFavoriteRequest;
import com.aiyige.model.response.MyFavoriteListResponse;
import com.aiyige.page.my.favorite.adapter.MyFavoriteAdapter;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.ToastX;
import com.aiyige.utils.widget.CommonDataView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Call;

public class MyFavoriteDataPage extends BaseFragment implements CommonDataView.RetrofitStub{

//    public static final int TYPE_PAGE_20 = 20;
//    public static final int TYPE_PAGE_5 = 5;
//    public static final int TYPE_PAGE_6 = 6;
//    public static final int TYPE_PAGE_7 = 7;
//    public static final int TYPE_PAGE_8 = 8;
//    public static final int TYPE_PAGE_9 = 9;

    private String subject = Subject.dynamic;
    private List<String> subjectList = new ArrayList<>();
    @BindView(R.id.cdv)
    CommonDataView cdv;
    MyFavoriteAdapter adapter;


    public MyFavoriteDataPage() {

    }

    public String getPageTitle() {
        return Subject.getSubjectName(subject);
    }

    public List<String> getSubject() {
        return subjectList;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public static MyFavoriteDataPage newInstance(String subject) {
        MyFavoriteDataPage fragment = new MyFavoriteDataPage();
        fragment.setSubject(subject);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.page_order_manager_data, container, false);
        ButterKnife.bind(this, view);
        adapter = new MyFavoriteAdapter();
        cdv.config(adapter, this);
        cdv.addItemDecoration(1, R.color.zwcolor_gray_e3e3e3);
        cdv.setRecyclerViewBgColor(ContextCompat.getColor(getActivity(), R.color.zwcolor_gray_f4f8f9));
//        setAutoRegisterEventBus(true);
        return view;
    }


    @Override
    public Call<ResponseBody> call(long currentPage) {
        ArrayList<String> list = new ArrayList<>();
        if (subject.equals(Subject.dynamic)) {
            list.addAll(Subject.getDynamicSubjects());
        }
        else if (subject.equals(Subject.video_course)) {
            list.add(Subject.video_course);
        }
        else  if (subject.equals(Subject.major_course)) {
            list.add(Subject.major_course);
        }
        else  if (subject.equals(Subject.training_course)) {
            list.add(Subject.training_course);
        }
        else  if (subject.equals(Subject.training_card)) {
            list.add(Subject.training_card);
        }else if (subject.equals(Subject.private_course)) {
            list.add(Subject.private_course);
        }
        else if (subject.equals(Subject.activity)) {
            list.add(Subject.activity);
        }
        return new MyFavoriteRequest.Builder()
                .pageNum(currentPage)
                .subject(list)
                .build();
    }

    @Override
    public CommonDataView.HandleResult handleResponse(String respJson, List resultList) {
        try {
            MyFavoriteListResponse homeResponse = JsonUtil.toObject(respJson, MyFavoriteListResponse.class);
            if (ListUtil.isEmpty(homeResponse.getContent())) {
                resultList.addAll(homeResponse.getContent());
                return new CommonDataView.HandleResult(homeResponse.isLast());
            } else {
                resultList.addAll(homeResponse.getContent());
                return new CommonDataView.HandleResult(homeResponse.isLast());
            }
        } catch (Exception e) {
            return new CommonDataView.HandleResult(e.getMessage());
        }

    }

    public void setEdit(boolean edit) {
        if (adapter != null) {
            adapter.setEdit(edit);
        }
    }

    public List<FavoriteRecord> getSelected() {
        if (adapter != null) {
            return adapter.getSelectedList();
        }
        return null;
    }

    public void deleteSelected() {
        if (adapter != null) {
            adapter.deleteSelected();
        }
    }

    public boolean checkEmptySelectList() {
        if (adapter == null) {
            return false;
        }
        if (adapter.getSelectedList() == null || adapter.getSelectedList().isEmpty()) {
            ToastX.show(MyApp.getAppContext().getString(R.string.please_select_favorite_data));
            return false;
        }
        return true;
    }

    public void selectAll() {
        if (adapter != null) {
            adapter.selectAll();
        }
    }

    public void unSelectAll() {
        if (adapter != null) {
            adapter.unSelectAll();
        }
    }

    @Override
    protected void initData() {
        cdv.doRefreshRequest();
    }

}
